<template>
	<view>
		<u-navbar title="修改资料" :placeholder='true'  :autoBack="true">
			<!-- <view class="rightslot" slot='right'>
				保存
			</view> -->
		</u-navbar>

		<view class="changeitem u-m-t-20">
			<text>用户名</text>
			<input type="text" v-model="info.nick_name" placeholder="请输入用户名" />
		</view>

		<view class="changeitem u-m-t-20" @click="show = true">
			<text>所在学校</text>
			<input type="text" v-model="info.school" placeholder="请选择所在学校 >" />
		</view>

		<view class="changeitem u-m-t-20">
			<text>手机号</text>
			<input type="text" v-model="info.phone" placeholder="请输入手机号" />
		</view>

		<view class="changeitem u-m-t-20" @click="phoneshow = true">
			<text>密码</text>
			<input type="text" v-model="pwd" placeholder="修改密码 >" />
		</view>

		<view class="changeitem u-m-t-20">
			<text>邮箱</text>
			<input type="text" v-model="info.email" placeholder="请输入邮箱" />
		</view>
		
		<view class="changeitem u-m-t-20">
			<text>设备号</text>
			<input type="text" v-model="info.sbh" placeholder="请输入设备号" />
		</view>
		
		
		<button class="savabtn" @click="rightClick">
			保存
		</button>

		<u-popup :show="show" @close="close" @open="open">
			<view class="u-p-30">
				<u-radio-group v-model="radiovalue1" placement="column" @change="groupChange">
					<u-radio :customStyle="{marginBottom: '18px'}" v-for="(item, index) in radiolist1" :key="index"
						:label="item.name" :name="item.id">
					</u-radio>
				</u-radio-group>
			</view>
		</u-popup>

		<u-popup :show="phoneshow" @close="phoneclose" mode='center' round='13' @open="phoneopen">
			<view class="u-p-30 " style="width: 686rpx;">
				<view class="flex-center">
					修改密码
				</view>
				<view class="u-m-tb-20">
					<text>旧密码</text>
					<input class="u-p-tb-20" type="text" v-model="oldpwd" placeholder="请输入" name="" id="">
				</view>
				<view class="u-m-tb-20">
					<text>新密码</text>
					<input class="u-p-tb-20" type="text" v-model="newpwd" placeholder="请输入" name="" id="">
				</view>
				<!-- <view class="u-m-tb-20">
					<text>确认密码</text>
					<input class="u-p-tb-20" type="text" v-model="repwd" placeholder="请输入" name="" id="">
				</view> -->

				<view class="twobtn flex-around">
					<view class="cancle" @click="phoneshow = false">
						取消
					</view>
					<view class="sure" @click="changepwd" style="color: #5697ff;">
						确认
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				radiolist1: [],
				radiovalue1: '榴莲',
				show: false,
				phoneshow: false,

				pwd: '',
				columns: [],
				oldpwd: '',
				newpwd: '',
				repwd: '',
				info: {
					nick_name: '',
					school: '',
					phone: '',
					email: '',
					sbh:'',
					schoolId:''
				}
			};
		},
		onLoad() {
			this.getschool()
			this.init()
		},
		methods: {
			init() {
				this.$api.get('/user/info', {}).then(res => {
					this.info = res.data
				}).catch(err => {
					this.$toast(err.message)

				})
			},
			changepwd() {
				this.$api.post('/user/updatePassword', {
					newPassword: this.newpwd,
					oldPassword: this.oldpwd
				}).then(res => {
					if (res.code == 200) {
						uni.setStorageSync('token', res.data.token)
						this.$toast(res.message)
						this.phoneshow = false
					}
				}).catch(res => {
					this.$toast(res.message)
				})
			},
			close() {
				this.show = false
			},
			open() {

			},
			phoneclose() {
				this.phoneshow = false
			},
			phoneopen() {

			},
			groupChange(n) {
				this.radiolist1.map(item => {
					if (item.id == n) {
						this.info.school = item.name

					}
				})
				
				this.info.schoolId = n
				this.show = false
			},

			getschool() {
				this.$api.get('/assistant/xSchool/listAllSchool', {}).then(res => {
					this.radiolist1 = res.data
				})
			},
			rightClick() {

				this.$api.post('/user/update', {
					email: this.info.email,
					nick_name: this.info.nick_name,
					phone: this.info.phone,
					school_id: this.info.schoolId,
					device_number:this.info.sbh
				}).then(res => {
					uni.setStorageSync('token', res.data.token)
					this.$toast(res.message)
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	.rightslot {
		background-color: #07C160;
		color: #fff;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		margin-right: 170rpx;
		font-size: 26rpx;
	}

	.changeitem {
		width: 100vw;
		box-sizing: border-box;
		background-color: #fff;
		padding: 40rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		input {
			text-align: right;
		}
	}
</style>